package com.example.aijiancece;

import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.textfield.TextInputEditText;

public class EditProfileActivity extends AppCompatActivity {

    private ShapeableImageView ivAvatar;
    private TextInputEditText etUsername;
    private TextInputEditText etEmail;
    private final ActivityResultLauncher<String> imageChooser = registerForActivityResult(
            new ActivityResultContracts.GetContent(),
            uri -> {
                if (uri != null) {
                    updateAvatar(uri);
                }
            }
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);

        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        ivAvatar = findViewById(R.id.ivAvatar);
        etUsername = findViewById(R.id.etUsername);
        etEmail = findViewById(R.id.etEmail);
        MaterialButton btnSave = findViewById(R.id.btnSave);

        toolbar.setNavigationOnClickListener(v -> finish());
        ivAvatar.setOnClickListener(v -> openImageChooser());
        btnSave.setOnClickListener(v -> saveProfile());

        loadProfile();
    }

    private void openImageChooser() {
        imageChooser.launch("image/*");
    }

    private void updateAvatar(Uri uri) {
        ivAvatar.setImageURI(uri);
        // TODO: 上传头像到服务器
    }

    private void loadProfile() {
        // TODO: 从SharedPreferences或API加载用户资料
    }

    private void saveProfile() {
        String username = etUsername.getText().toString();
        String email = etEmail.getText().toString();

        if (username.isEmpty() || email.isEmpty()) {
            Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO: 保存用户资料到服务器
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }
} 